#include "customchartview.h"

#include <QChart>
#include <QValueAxis>

#include "controller.h"
#include "communicationproxy.h"

CustomChartView::CustomChartView(QWidget *parent)
    : QtCharts::QChartView(parent)
{

    m_series = new QtCharts::QSplineSeries;

    QtCharts::QChart *chart = new QtCharts::QChart();
    chart->legend()->hide();
    chart->setTitle(tr("Fuzzy PID Test"));

    QtCharts::QValueAxis *xAxis = new QtCharts::QValueAxis();
    xAxis->setRange(0, 100);

    QtCharts::QValueAxis *yAxis = new QtCharts::QValueAxis();
    yAxis->setRange(0, 120);

    chart->addAxis(xAxis, Qt::AlignBottom);
    chart->addAxis(yAxis, Qt::AlignLeft);

    chart->addSeries(m_series);

    setChart(chart);


    connect(CInstance->communicationProxy()->communication(), &Communication::stateChanged
            , this, &CustomChartView::onSrialPortStateChanged);
    connect(CInstance->communicationProxy()->communication(), &Communication::temperatureChanged
            , this, &CustomChartView::onTemperatureChanged);

    onSrialPortStateChanged(Communication::UnconnectedState);
}

void CustomChartView::onTemperatureChanged(float v)
{
    m_series->append(m_series->count(), v);
}

void CustomChartView::onSrialPortStateChanged(int state)
{
    switch (state) {
    case Communication::ConnectedState:
        m_series->clear();
        break;
    default:
        break;
    }
}
